package com.rest.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rest.ApiEnum;
import com.rest.EnumsService;
import com.util.LoadPackage;
import com.zyncro.api.IGroupApiService;
import com.zyncro.api.model.IApiType;

public class EnumsServiceImpl implements EnumsService {

	private final Map<String, List<ApiEnum>> enums = new HashMap<String, List<ApiEnum>>();

	public EnumsServiceImpl() {
		String jarName = IGroupApiService.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		List<Class<? extends Enum<?>>> list = LoadPackage.getEnums(jarName, "com.zyncro.api.model");
		for (Class<? extends Enum<?>> aClass : list) {
			List<ApiEnum> enums = new ArrayList<ApiEnum>();
			Enum<?>[] values = aClass.getEnumConstants();
			for (Enum<?> value : values) {
				if (IApiType.class.isAssignableFrom(value.getClass())) {
					IApiType apiTypeEnum = (IApiType) value;
					enums.add(new ApiEnum(value.name(), apiTypeEnum.getId()));
				}
			}
			this.enums.put(aClass.getSimpleName(), enums);
		}
	}

	@Override
	public Map<String, List<ApiEnum>> getEnumerations() {
		return enums;
	}

}
